$(function () {
    $.post("user/cookieLogin",{}, function (json) {
        if (json == "home.jsp"){
            location.href = "home.jsp";
        }
        if (json == "userHome.jsp"){
            location.href = "home.jsp";
        }
    });
    $.post("user/sessionLogin",{}, function (json) {
        if (json.flag && json.type) {
            location.href = "home.jsp";
        }else if (json.flag){
            location.href = "userHome.jsp";
        }
    });
    //用户名验证
    $("#name").blur(chenckUsername);
    function chenckUsername() {
        var namevalue = $("#name").val();
        var reg = /(^[a-zA-Z0-9_-]{4,18})|(^[\u2E80-\u9FFF]{2,5})/;
        var flag = reg.test(namevalue);
        if (flag){
            show_check("#name","success","");
        }else {
            show_check("#name","error","用户名可为2-5位汉字或4-18个英文");
        }
        return flag;
    }
    //密码验证
    $("#password").blur(chenckPassword);
    function chenckPassword() {
        var passwordvalue = $("#password").val();
        var reg = /^[a-zA-Z0-9_-]{4,18}$/;
        var flag = reg.test(passwordvalue);
        if (flag){
            show_check("#password","success","");
        }else {
            show_check("#password","error","密码可为4-18位英文和数字的组合");
        }
        return flag;
    }

    //验证码
    $("#code").blur(chenckCheck);
    function chenckCheck() {
        var checkValue = $("#code").val();
        var reg = /^[a-zA-Z0-9_-]{5,5}$/;
        var flag = reg.test(checkValue);
        if (flag){
            $("#code").parent().addClass("has-success");
            $("#span").text("")
        }else {
            $("#code").parent().addClass("has-error");
            $("#span").text("验证码错误")
        }
        return flag;
    }

    function show_check(opt,state,msg){
        //清除当前元素状态
        $(opt).parent().removeClass("has-success has-error");
        $(opt).next("span").text("");
        if ("success" == state){
            $(opt).parent().addClass("has-success");
            $(opt).next("span").text(msg)
        }else if ("error" == state){
            $(opt).parent().addClass("has-error")
            $(opt).next("span").text(msg)
        }
    }

    $("#submit_login").click(function () {
        var yh = $("#yh").val();
        if (chenckUsername() && chenckPassword() && chenckCheck()) {
            $.get("user/codeCheck",{code:$("#code").val()}, function (json) {
                if (json){
                    $("#ts").html("正在登陆...");
                    if (yh == 2){
                        $.post("user/adminLogin", $("#loginForm").serialize(), function (json) {
                            if (json=="home.jsp"){
                                location.href = json
                            }else {
                                $("#ts").html("用户名或密码错误！");
                            }
                        });
                    }else if (yh == 1){
                        $.post("user/userLogin", $("#loginForm").serialize(), function (json) {
                            if (json !=""){
                                location.href = "userHome.jsp"
                            }else {
                                $("#ts").html("用户名或密码错误！");
                            }
                        });
                    }
                }else {
                    $("#code").parent().addClass("has-error");
                    $("#span").html("验证码错误")
                }
            });
        }
    });


})